-- Copyright (c) 2024 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at:
--         http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.
--
-- Create: 2025-7-21
-- Description: common interfaces.
local log = require 'mc.logging'

local module = {}
module.__index = module

function module.pcall_read(obj, prop)
    local ok, res = pcall(function() 
        return obj[prop]
    end)
    if not ok then
        log:info('get prop [%s] failed [%s]', prop, res)
        return nil
    end
    return res
end

function module.pcall_write(obj, prop, value)
    local ok, res = pcall(function() 
        obj[prop] = value
    end)
    if not ok then
        log:info('set prop [%s] failed [%s]', prop, res)
    end
    return ok
end

local compare_handler = {
    [1] = function(reading, condition)
        return reading < condition
    end,
    [2] = function(reading, condition)
        return reading <= condition
    end,
    [3] = function(reading, condition)
        return reading > condition
    end,
    [4] = function(reading, condition)
        return reading >= condition
    end,
    [5] = function(reading, condition)
        return reading == condition
    end,
    [6] = function(reading, condition)
        return reading ~= condition
    end
}

function module.compare(operator, reading, condition)
    return compare_handler[operator](reading, condition)
end

return module